﻿using Dycox.Collections;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace Dycox.ComponentModel
{
    public class ViewGroup : ViewObject
    {
        public ViewGroup(string name)
            : base(name)
        {

        }

        internal ViewGroup(XElement elt)
            : this((string)elt.Attribute("name"))
        {
        }

        internal ViewGroup(ViewGroup other)
            : this(other.Name)
        {
        }

        public int GetFieldCount(bool includeSubGroups)
        {
            int count = 0;

            if (includeSubGroups)
            {
                Action<ViewField, object> callback = (f, s) =>
                {
                    count++;
                };

                WalkFields(this, callback, null);
            }
            else if (_items != null)
                return _items.Count(o => o is ViewField);

            return count;
        }

        public ViewField[] GetFields(bool includeSubGroups)
        {
            if (_items != null)
            {
                if (includeSubGroups)
                {
                    List<ViewField> list = new List<ViewField>();
                    WalkFields(this, (fld, s) =>
                        {
                            list.Add(fld);
                        }, null);
                    return list.ToArray();
                }
                else
                    return _items.OfType<ViewField>().ToArray();
            }
            else
                return new ViewField[0];
        }

        public void WalkFields(Action<ViewField, object> callback)
        {
            WalkFields(callback, null);
        }

        public void WalkFields(Action<ViewField, object> callback, object state)
        {
            if (callback == null)
                throw new ArgumentNullException("callback");

            WalkFields(this, callback, state);
        }

        protected static void WalkFields(ViewGroup group, Action<ViewField, object> callback, object state)
        {
            if (group._items != null)
            {
                foreach (var item in group._items)
                {
                    if (item is ViewField)
                        callback((ViewField)item, state);
                    else
                        WalkFields((ViewGroup)item, callback, state);
                }
            }

        }

        public ViewGroup[] GetGroups()
        {
            if (_items == null) return new ViewGroup[0];
            else return _items.OfType<ViewGroup>().ToArray();
        }

        private Collection _items;

        public Collection Items
        {
            get
            {
                if (_items == null)
                    _items = new Collection(this);

                return _items;
            }
        }

    }
}
